package com.study.leetcode;

public class Solution_66 {
    public static int[] plusOne(int[] digits) {
        int last = digits.length - 1;
        digits[last] = digits[last] + 1;
        int flag = 0;
        for (int i = last; i >= 0; i--) {
            if (flag > 0) {
                digits[i] = digits[i] + flag;
                flag = 0;
            }
            if (digits[i] >= 10) {
                digits[i] = digits[i] % 10;
                flag = 1;
            }
        }
        if (flag > 0) {
            int[] result = new int[digits.length + 1];
            for (int i = 0; i < digits.length; i++) {
                result[i + 1] = digits[i];
            }
            result[0] = flag;
            return result;
        }
        return digits;
    }

    public static void main(String[] args) {
        int[] arr = {9};
        int[] ints = plusOne(arr);
        for (int i : ints) {
            System.out.println(i);
        }

    }
}
